package com.example.answer_staff;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.gaosai.staff.utils.CommonUtils;
import com.gaosai.staff.view.CustomFlowLayout;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ListQuizDetailsActivity  extends Activity{
	private LayoutInflater mInflater ;
	private DisplayImageOptions options;
	private Button quiz_button;
	private ImageView quiz_detail_backbutton;
	private ImageView quiz_detail_imageView;
	private TextView quiz_detail_title;
	private TextView quiz_detail_content;
	private TextView quiz_detail_time;
	private TextView quiz_detail_date;
	private TextView quiz_detail_venue;
	private CustomFlowLayout quiz_detail_flowlayout;
	private String getActivityData;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.quiz_details);
		setupView();
		initData() ;
		setlisteners();
	}

	private void setlisteners() {

		quiz_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {

				Intent  intent=new Intent(ListQuizDetailsActivity.this,AnswerActivity.class);
				intent.putExtra("application_list", getActivityData);
				startActivity(intent);

			}
		});

		quiz_detail_backbutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});


	}

	private void initData() {
		try {
			JSONObject jsonObject = new JSONObject(getActivityData) ;
			ImageLoader.getInstance().displayImage(jsonObject.optString("banner"), quiz_detail_imageView,options) ;
			quiz_detail_title.setText(jsonObject.optString("subject")) ;
			quiz_detail_content.setText(jsonObject.optString("briefContent")) ;
			quiz_detail_time.setText(String.format(jsonObject.optInt("startTime")+"%s", "-"+jsonObject.optInt("endTime"))) ;
			quiz_detail_date.setText("2014-45-98") ;
			quiz_detail_venue.setText(jsonObject.getString("venue")) ;

			JSONArray flowLayoutArray = jsonObject.optJSONArray("tags") ;
			quiz_detail_flowlayout.removeAllViews() ;

			for (int i = 0; i < flowLayoutArray.length(); i++) {
				TextView tv = (TextView) mInflater.inflate(R.layout.tv,quiz_detail_flowlayout, false);
				String tagName = flowLayoutArray.optJSONObject(i).optString("name") ;
				//取得缩写名字
				String abbreName = CommonUtils.getAbbreviation(tagName) ;
				tv.setText(abbreName) ;
				CommonUtils.setColor(abbreName,tv,ListQuizDetailsActivity.this) ;
				quiz_detail_flowlayout.addView(tv) ;
			}

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void setupView() {
		mInflater = LayoutInflater.from(this) ;
		getActivityData = this.getIntent().getStringExtra("new") ;
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.ic_launcher)
		.showImageForEmptyUri(R.drawable.ic_launcher)
		.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
		.cacheOnDisc(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
		quiz_button=(Button)findViewById(R.id.quiz_bt);
		quiz_detail_backbutton=(ImageView)findViewById(R.id.quiz_detail_backbutton);
		quiz_detail_imageView = (ImageView) this.findViewById(R.id.quiz_detail_imageView) ;
		quiz_detail_title = (TextView) this.findViewById(R.id.quiz_detail_title) ;
		quiz_detail_content = (TextView) this.findViewById(R.id.quiz_detail_content) ;
		quiz_detail_time = (TextView) this.findViewById(R.id.quiz_detail_time) ;
		quiz_detail_date = (TextView) this.findViewById(R.id.quiz_detail_date) ;
		quiz_detail_venue = (TextView) this.findViewById(R.id.quiz_detail_venue) ;
		quiz_detail_flowlayout = (CustomFlowLayout) this.findViewById(R.id.quiz_detail_flowlayout) ;




	}

}
